<!--  故障类型  故障现象-->
<template>
  <basic-container class="production-page">
    <avue-tabs ref="tabs" :option="tabOption" @change="handleChange"> </avue-tabs>
    <!--  故障现象 -->
    <span v-if="tabType.prop === 'faultPhenomenon'">
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :defaults.sync="defaults"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >
        <template slot="menuLeft">
          <el-button type="danger" plain size="small" icon="el-icon-delete" v-if="permission.faultType_delete" @click="handleDel">删除</el-button>
          <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
        </template>
      </avue-crud>
    </span>
    <!--  故障类型 -->
    <span v-else-if="tabType.prop === 'faultType'">
      <faultSubType />
    </span>
    <export-comp ref="exportComp" page-api-url="mes-product/mro/dict/page" :option="option" :page="page" :query="query" file-name="故障现象"></export-comp>
  </basic-container>
</template>

<script>
import faultSubType from './faultSubType.vue';
import { getList, add, update, remove } from '@/api/equipment-manage/faultType';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
export default {
  components: {
    faultSubType,
    exportComp
  },
  data() {
    return {
      form: {},
      defaults: {},
      query: {
        dictType: 'EQUIPMENT',
        subType: 'FAULT'
      },
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 500px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        viewBtnText: '详情',
        viewTitle: '详情',
        excelBtn: true, // 导出
        delBtn: false,
        selection: true,
        dialogClickModal: false,
        labelWidth: 120,
        menuWidth: 300,
        column: [
          {
            label: '故障现象代码',
            prop: 'code',
            search: true,
            span: 24,
            // maxlength: 20,
            // showWordLimit: true,
            searchLabelWidth: 120,
            rules: [
              {
                required: true,
                message: '请输入故障现象代码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '故障现象名称',
            prop: 'name',
            search: true,
            span: 24,
            searchLabelWidth: 120,
            rules: [
              {
                required: true,
                message: '请输入故障现象名称',
                trigger: 'blur'
              }
            ]
          }
        ]
      },
      data: [],
      tabType: {},
      tabOption: {
        column: [
          {
            label: '故障现象',
            prop: 'faultPhenomenon'
          },
          {
            label: '故障类型',
            prop: 'faultType'
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.faultType_add, false),
        viewBtn: this.vaildData(this.permission.faultType_view, false),
        delBtn: this.vaildData(this.permission.faultType_delete, false),
        editBtn: this.vaildData(this.permission.faultType_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
    tabName() {
      return this.tabType.prop === 'faultPhenomenon' ? 'faultPhenomenon' : 'faultType';
    }
  },
  mounted() {
    this.tabType = this.tabOption.column[0];
    this.$nextTick(() => {
      this.$structuralSplit(this.tabType.label);
    });
  },
  methods: {
    rowSave(row, done, loading) {
      row.dictType = 'EQUIPMENT';
      row.subType = 'FAULT';
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '添加故障现象成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      row.dictType = 'EQUIPMENT';
      row.subType = 'FAULT';
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '编辑故障现象成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    handleDel() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud && this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleChange(column) {
      this.tabType = column;
      this.$nextTick(() => {
        this.$structuralSplit(this.tabType.label);
      });
    }
  }
};
</script>

<style lang="scss" scope>
.production-page .avue-group__header {
  display: none;
}
</style>
